package com.yellowpage.wf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.wf.dao.WorkflowPathDao;
import com.yellowpage.wf.data.WorkflowDataUtils;
import com.yellowpage.wf.data.WorkflowNode;
import com.yellowpage.wf.data.WorkflowPath;

public class WorkflowPathServiceImpl extends BaseServiceImpl implements
		WorkflowPathService {
	private WorkflowPathDao workflowPathDao;

	public void setWorkflowPathDao(WorkflowPathDao workflowPathDao) {
		this.workflowPathDao = workflowPathDao;
	}

	/* White generate Method */
	public WorkflowPath getWorkflowPath(Long id) {
		return workflowPathDao.get(id);
	}

	public List<WorkflowPath> getAllWorkflowPath() {
		return workflowPathDao.getAll();
	}

	public List<Map> queryWorkflowPathByPage(Map queryMap, PageRender pageRender) {

		String sql = "SELECT a.WORKFLOW_PATH_ID , a.WORKFLOW_ID , a.NODE_FROM , b.name NODE_FROM_NAME, a.NODE_TO ,c.name NODE_TO_NAME, a.WORKFLOW_PATH_STATUS , "
				+ " a.CREATE_TIME , a.LAST_UPDATE_TIME , a.NAME , a.DISPLAY_NAME, "
				+ " (select distinct decode(e.name, null, '系统管理员',e.name)" 
				+ " from employee e, WF_WORKFLOW_PATH wwp" 
				+ " where wwp.create_user = e.employee_id(+) and wwp.workflow_path_id = a.workflow_path_id) as CREATE_USER, "
				+ " (select distinct decode(e.name, null, '系统管理员',e.name)" 
				+ " from employee e, WF_WORKFLOW_PATH wwp where wwp.last_update_user = e.employee_id(+) "
				+ " and wwp.workflow_path_id = a.workflow_path_id) as LAST_UPDATE_USER"
				+ " FROM WF_WORKFLOW_PATH a , WF_WORKFLOW_NODE b ,WF_WORKFLOW_NODE c "
				+ " WHERE 1=1 and a.NODE_FROM=b.workflow_node_id(+) and a.NODE_TO=c.workflow_node_id(+) ";
		if (queryMap.get("nodeFromType") != null
				&& queryMap.get("nodeFromType").equals(
						WorkflowDataUtils.NODE_TYPE_START)) {
			sql = sql + " and b.type='" + WorkflowDataUtils.NODE_TYPE_START
					+ "'";
			queryMap.remove("nodeFromType");
		}
		if (queryMap.get("nodeToType") != null
				&& queryMap.get("nodeToType").equals(
						WorkflowDataUtils.NODE_TYPE_END)) {
			sql = sql + " and c.type='" + WorkflowDataUtils.NODE_TYPE_END + "'";
			queryMap.remove("nodeToType");
		}
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(sql)
				.addCondition(" and WORKFLOW_PATH_ID = :workflowPathId ")
				.addCondition(" and a.WORKFLOW_ID = :workflowId ")
				.addCondition(" and a.NODE_FROM = :nodeFrom ")
				.addCondition(" and a.NODE_TO = :nodeTo ")
				.addCondition(" and a.WORKFLOW_PATH_STATUS like :workflowPathStatus ")
				.addCondition(" and a.NAME like :name ").
				addCondition(" and a.DISPLAY_NAME like :displayName ");
				sqlAssistant.setTail(" ORDER BY a.WORKFLOW_PATH_ID DESC ");

		List<Map> resultList = sqlAssistant.query(
				baseDao.getConnection(), queryMap);

		return resultList;
	}

	public void saveWorkflowPath(WorkflowPath workflowPath) {
		workflowPath.setWorkflowPathStatus(WorkflowDataUtils.ENTITY_STATUS_ON);
		workflowPathDao.save(workflowPath);
	}	
	

	public void updateWorkflowPath(WorkflowPath workflowPath) {
		workflowPathDao.update(workflowPath);
	}

	public void delete(List deleteList) {
		workflowPathDao.remove(deleteList);
	}

	

	public Map findNode(Long id) {
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(" ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		if (resultList != null && resultList.size() > 0) {
			return resultList.get(0);
		} else {
			return null;
		}
	}

	

}